﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace Login5173
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.webBrowser2.ScriptErrorsSuppressed = true;
        }
        CookieContainer httpCookie = new CookieContainer();

        private void button1_Click(object sender, EventArgs e)
        {
            var cookie = FullWebBrowserCookie.GetCookieInternal(this.webBrowser1.Url, true);
            //webBrowser1.Cookie();

            var cookieArr = cookie.Split(';');
            foreach (var item in cookieArr)
            {
                var arr = item.Split('=');
                if (arr != null && arr[0].Trim().Equals(".5173auth", StringComparison.OrdinalIgnoreCase))
                {
                    var coo = new Cookie();
                    coo.Name = ".5173auth";
                    coo.HttpOnly = true;
                    coo.Domain = ".5173.com";
                    coo.Value = arr[1].Trim();
                    httpCookie.Add(coo);
                    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://user.5173.com/");
                    request.CookieContainer = httpCookie;
                    var readStream = new StreamReader(request.GetResponse().GetResponseStream());
                    this.webBrowser2.DocumentText = readStream.ReadToEnd();
                    MessageBox.Show("cookie ok");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var url = "http://trading.5173.com/list/myorder_manage.aspx";
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.CookieContainer = httpCookie;
            var readStream = new StreamReader(request.GetResponse().GetResponseStream());
            this.webBrowser2.DocumentText = readStream.ReadToEnd();
        }
    }

    public class MyWebBrowser : WebBrowser
    {
        /// <summary>
        ///  Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1
        /// </summary>
        /// <returns></returns>
        public string Cookie()
        {
            if (this.Url == null)
                return null;
            string dir = this.Url.Host;
            FileStream fr = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Cookies) + "\\index.dat", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            byte[] __dat = new byte[(int)fr.Length];
            fr.Read(__dat, 0, __dat.Length);
            fr.Close();
            fr.Dispose();
            string __datstream = Encoding.Default.GetString(__dat);
            int p1 = 0;
            p1 = __datstream.IndexOf("@" + dir, p1);
            if (p1 == -1)
                p1 = __datstream.IndexOf("@" + dir.Substring(dir.IndexOf('.') + 1));
            if (p1 == -1)
                return this.Document.Cookie;
            int p2 = __datstream.IndexOf(".txt", p1 + 1);
            p1 = __datstream.LastIndexOf('@', p2);
            string dm = __datstream.Substring(p1 + 1, p2 - p1 + 3).TrimStart('?');
            p1 = __datstream.LastIndexOf(":", p1);
            p2 = __datstream.IndexOf('@', ++p1);
            __datstream = string.Format("{0}@{1}", __datstream.Substring(p1, p2 - p1), dm);

            Dictionary<string, string> __cookiedicts = new Dictionary<string, string>();
            string __n;
            StringBuilder __cookies = new StringBuilder();
            __datstream = File.ReadAllText(Environment.GetFolderPath(Environment.SpecialFolder.Cookies) + "\\" + __datstream, Encoding.Default);
            p1 = -2;
            do
            {
                p1 += 2;
                p2 = __datstream.IndexOf('\n', p1);
                if (p2 == -1)
                    break;
                __n = __datstream.Substring(p1, p2 - p1);
                p1 = p2 + 1;
                p2 = __datstream.IndexOf('\n', p1);
                if (!__cookiedicts.ContainsKey(__n))
                    __cookiedicts.Add(__n, __datstream.Substring(p1, p2 - p1));
            }
            while ((p1 = __datstream.IndexOf("*\n", p1)) > -1);
            if (this.Document.Cookie != null && this.Document.Cookie.Length > 0)
            {
                foreach (string s in this.Document.Cookie.Split(';'))
                {
                    p1 = s.IndexOf('=');
                    if (p1 == -1)
                        continue;
                    __datstream = s.Substring(0, p1).TrimStart();
                    if (__cookiedicts.ContainsKey(__datstream))
                        __cookiedicts[__datstream] = s.Substring(p1 + 1);
                    else
                        __cookiedicts.Add(__datstream, s.Substring(p1 + 1));
                }
            }
            foreach (string s in __cookiedicts.Keys)
            {
                if (__cookies.Length > 0)
                    __cookies.Append(';');
                __cookies.Append(s);
                __cookies.Append('=');
                __cookies.Append(__cookiedicts[s]);
            }
            return __cookies.ToString();
        }
    }
}
